// posts.js
var util = require('../../utils/util.js');
var dbUtil = require('../../utils/db.js');
var gitee = require('../../utils/gitee.js');
var github = require('../../utils/github.js');
var WxParse = require('../../utils/wxParse/wxParse.js');

//获取应用实例
const app = getApp();

const db = wx.cloud.database();
var topic = null;
Page({
  data: {
    topic: {}
  },

  onLoad: function (options) {
    let that = this;
    let tempTopic = JSON.parse(options.data);

    var url = tempTopic.url;
    tempTopic.url = tempTopic.url.replace("m.gitee","gitee");
    this.setData({
      topic: tempTopic
    });
    if(null==url || ""==url)
    {
      return;
    }

    //如果是码云，去地址抓取
    if(url.indexOf("gitee.com")>0)
    {
      //抓取码云页面
      wx.request({
        url: url +"/overview_data.json",
        success: function (res) {
          var html = res.data.readme.content;
          var imgArr = gitee.filterImg(html);
          //把相对路径的图片tag去掉。
          html = html.replace(/<img src=\"\/([\s\S]*?)>/g,"");

          //如果不是jeexjj开源，把QQ群过滤掉。
          if (url.indexOf("jeexjj") < 0)
          {
            //把QQ群过滤掉。
            html = html.replace(/QQ([\s\S]*?)[0-9]{6,12}/g, "");
            html = html.replace(/qq([\s\S]*?)[0-9]{6,12}/g, "");
            html = html.replace(/[0-9]{6,12}/g, "");
          }
          try{
            WxParse.wxParse('article', 'html', html, that, 5);
          }catch(e){
            
            //把<pre>*</pre>截掉。解析html异常
            html = html.replace(/<pre>([\s\S]*?)<\/pre>/g, "");
            try {
              WxParse.wxParse('article', 'html', html, that, 5);
            }catch (e2) {
              var info = "<p>查询详情失败,请去开源网址查询吧。</p>";
              WxParse.wxParse('article', 'html', info, that, 5);
            }

          }
         
          if (null == tempTopic.imgs)
          {
            tempTopic.imgs = [];
          }

          tempTopic.imgs = tempTopic.imgs.concat(imgArr);
          that.setData({
            topic: tempTopic
          });
        },
        fail: function (error) {
          console.error(error)// 服务器回包信息
        },
      })
    }

    // if is coding.net
    if (url.indexOf("coding.net") > 0) {
      wx.request({
        url: url,
        success: function (res) {
          var html = res.data.data.file.data;
          WxParse.wxParse('article', 'html', html, that, 5);

        },
        fail: function (error) {

        },
      })
    }

    //如果是github，去地址抓取
    if (url.indexOf("github.com") > 0) {
      wx.request({
        url: url + "/blob/master/README.md",
        success: function (res) {
          var html = res.data
          
          //过滤掉冗余无价值的信息
          var htmlMatch = html.match(/<article([\s\S]*?)<\/article>/g);
          if (null != htmlMatch)
          {
            html = htmlMatch[0];
          }

          WxParse.wxParse('article', 'html', html, that, 5);

          var imgArr = github.filterImg(html);
          if (null == tempTopic.imgs) {
            tempTopic.imgs = [];
          }
          tempTopic.imgs = tempTopic.imgs.concat(imgArr);
          that.setData({
            topic: tempTopic
          });
        },
        fail: function (error) {
          console.error(error)// 服务器回包信息
        },
      })
    }
  },

  //预览图片
  previewImage: function (e) {
    let tempFiles = [].concat(this.data.topic.headImg).concat(this.data.topic.imgs);

    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls: tempFiles // 需要预览的图片http链接列表
    })
  }

})
